package com.eternal.base.data.source;

import com.eternal.base.concat.DeviceModel;
import com.eternal.base.concat.DeviceModelInfo;
import com.eternal.base.concat.NotificationName;
import com.eternal.base.database.BaseDatabase;
import com.eternal.base.database.dao.DeviceDao;
import com.eternal.base.database.dao.NotificationDao;
import com.eternal.base.database.entity.Notification;
import com.eternal.base.global.BluetoothKey;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.Single;
import io.reactivex.SingleEmitter;
import io.reactivex.SingleOnSubscribe;

public class NotificationSource implements INotificationSource {
    private static final String DEFAULT_AUTOMATIC_NAME = "Automations";
    private static final String DEFAULT_NOTIFICATIONS = "Notifications";
    private static final String DEFAULT_ALARMS = "Alarms";

    private DeviceDao dDao;
    private NotificationDao nDao;

    public NotificationSource() {
        dDao = BaseDatabase.getInstance().deviceDao();
        nDao = BaseDatabase.getInstance().notificationDao();
    }

    @Override
    public void setNotification(Notification notification) {
        nDao.update(notification);
    }

    @Override
    public void addNotification(Notification notification) {
        nDao.insertNotification(notification);
    }

    @Override
    public List<Notification> getNotifications(String mac, byte port) {
        return nDao.loadMacAll(mac,port);
    }

    @Override
    public void deleteNotification(String mac,byte port, int id, byte type) {
        nDao.deleteNotification(mac, port, id, type);
    }

    @Override
    public void setNotificationOpen(String mac, byte port, int id, byte type, boolean open) {
        nDao.updateOpen(mac,port, id, type, open);
    }

    @Override
    public Notification getNotification(String mac, byte port, int id, byte type) {
        return null;
    }

    @Override
    public List<NotificationName> getNotificationNames(String mac ,byte port) {
        return null;
    }

    @Override
    public DeviceModelInfo getModelInfo(String mac) {
        return dDao.loadModelInfo(mac);
    }

    @Override
    public Boolean nameExists(String mac, byte port, String name) {
        return nDao.loadNameByMac(mac,port, name) != null;
    }

    @Override
    public void create(Notification notification) {
        List<NotificationName> names = nDao.loadMacAllName(notification.mac,notification.port);
        List<Integer> numbers = new ArrayList<>();
        if (notification.name == null) {
            notification.name = getDefaultName(notification, names);
        }
        for (NotificationName name : names) {
            if (name.type == notification.type) {
                numbers.add(name.id);
            }
        }
        Collections.sort(numbers);
        int i = 0;
        for (; i < numbers.size(); i++) {
            if (numbers.get(i) != i) {
                break;
            }
        }
        notification.id = i;
    }

    private String getDefaultName(Notification notification, List<NotificationName> names) {
        List<Integer> nameIndex = new ArrayList<>();
        String defaultName;
        if (notification.type == BluetoothKey.ALARMS) {
            defaultName = DEFAULT_ALARMS;
        } else if (notification.type == BluetoothKey.AUTOMATION) {
            defaultName = DEFAULT_AUTOMATIC_NAME;
        } else {
            defaultName = DEFAULT_NOTIFICATIONS;
        }
        for (NotificationName name : names) {
            if (name.type == notification.type) {
                MinNumberUtil.getNumber(nameIndex, defaultName, name.name);
            }
        }
        int num = MinNumberUtil.minNumberZero(nameIndex);
        return (num == 0 ? defaultName : defaultName + " " + num);
    }
}
